package org.bootstrap.response;

import cn.hutool.core.lang.Console;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * http 返回数据统一
 */
@Component
@Lazy
public class ResponseBody {
    private static final JSONObject json = new JSONObject();

    private static void clear() {
        if (!json.isEmpty()) {
            json.clear();
        }
    }

    public ResponseBody http_ok(int code) {
        clear();
        json.putOpt("code", code);
        return this;
    }

    public JSONObject setMsg(String msg) {
        json.putOpt("msg", msg);
        return json;
    }

    public JSONObject setList(List<? extends List<Object>> list) {
        try {
            json.putOpt("list", JSONUtil.parseArray(list));
        } catch (Exception e) {
            Console.error(e);
        }
        return json;
    }

}
